﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.ObjectMapping;

namespace WeChat.CMS.Foods
{
    [RemoteService(false)]
    public class FoodAppService : CMSAppService, IFoodAppService
    {
        private readonly IFoodRepository _foodRepository;

        public FoodAppService(IFoodRepository foodRepository)
        {
            this._foodRepository = foodRepository;
        }
        public async Task<bool> CreateAsync(FoodCreationDto foodCreation)
        {
            Guid foodId = GuidGenerator.Create();
            Food food = new Food(foodId)
            {
                Name = foodCreation.Name,
                Price = foodCreation.Price,
                Description = foodCreation.Description,
                PreviewImageId = foodCreation.PreviewImageId,
            };
            await _foodRepository.InsertAsync(food);
            return true;
        }

        public async Task<bool> UpdateAsync(FoodUpdateDto foodUpdate)
        {
            var foodDetail = await _foodRepository.FindAsync(f => f.Id.Equals(foodUpdate.Id));
            if (foodDetail != null)
            {
                foodDetail.Name = foodUpdate.Name;
                foodDetail.Price = foodUpdate.Price;
                foodDetail.Description = foodUpdate.Description;
                foodDetail.PreviewImageId = foodUpdate.PreviewImageId;
                await _foodRepository.UpdateAsync(foodDetail);
                return true;
            }
            else
            {
                throw new UserFriendlyException($"菜品ID不正确");
            }
        }


        public async Task<bool> DeleteAsync(Guid Id)
        {
            var foodDetail = await _foodRepository.FindAsync(f => f.Id.Equals(Id));
            if (foodDetail != null)
            {
                await _foodRepository.DeleteAsync(foodDetail);
                return true;
            }
            else
            {
                throw new UserFriendlyException($"菜品ID不正确");
            }
        }

        public async Task<FoodDto> GetAsync(Guid id)
        {
            var res = await _foodRepository.FindAsync(x => x.Id == id);
            if (res == null)
            {
                throw new UserFriendlyException($"该菜品不存在，请检查");
            }
            return ObjectMapper.Map<Food, FoodDto>(res);
        }
        public async Task<List<FoodDto>> GetAllListAsync()
        {
            var res = await _foodRepository.GetListAsync();
            return ObjectMapper.Map<List<Food>, List<FoodDto>>(res);
        }

        public async Task<PagedResultDto<FoodDto>> GetListAsync(FoodPagedListDto input)
        {
            var foodQueryable = (await _foodRepository.GetQueryableAsync())
                .WhereIf(!string.IsNullOrEmpty(input.Name), f => f.Name.Contains(input.Name));
            var list = foodQueryable.Skip(input.SkipCount).Take(input.MaxResultCount)
                .OrderByDescending(f=>f.LastModificationTime!=null?f.LastModificationTime:f.CreationTime)
                .ToList();
            
            var count= foodQueryable.Count();
            return new PagedResultDto<FoodDto>(count,ObjectMapper.Map<List<Food>,List<FoodDto>>(list));
        }
    }
}
